//
//  TravelTopicDeBtnTableViewCell.m
//  ToWhere
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 www.TE.com. All rights reserved.
//

#import "TravelTopicDeBtnTableViewCell.h"
#import "TravelTopicDetailsOneTableViewCell.h"
#import <SDWebImageManager.h>
#import <UIButton+WebCache.h>

@interface TravelTopicDeBtnTableViewCell ()

@property (nonatomic, assign) CGFloat wd;
@property (nonatomic, assign) CGFloat kh;
@property (nonatomic, copy) NSString *strI;
@property (nonatomic, retain) UILabel *labelDes;


@end


@implementation TravelTopicDeBtnTableViewCell
-(void)dealloc
{
    [_btnName release];
    [_model release];
    [_btnPic release];
    [_labelDes release];
    
    [_strI release];
    
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
    
}

-(void)createView
{
    self.btnPic = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self addSubview:self.btnPic];
    
    self.labelDes = [[UILabel alloc]init];
    self.labelDes.numberOfLines = 0;
    self.labelDes.font = [UIFont systemFontOfSize:15 *kWidth];
    self.labelDes.textColor = [UIColor colorWithWhite:0.256 alpha:1.000];
    
    //  self.labelDes.backgroundColor = [UIColor purpleColor];
    [self addSubview:self.labelDes];
    [_labelDes release];
    self.btnName = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.btnName.backgroundColor = [UIColor lightGrayColor];
   
    self.btnName.titleLabel.font = [UIFont systemFontOfSize:15 *kWidth];
     [self addSubview:self.btnName];
    
    
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat h = (int)(self.kh / self.wd * (self.frame.size.width ));
    self.btnPic.frame = CGRectMake(5 * kWidth, 5 *kHeight, self.frame.size.width - 10 *kWidth, h);
    
    
    self.labelDes.frame = CGRectMake(10 *kWidth, self.btnPic.frame.size.height + 10 *kWidth, self.frame.size.width - 20 * kWidth, [[TravelTopicDetailsOneTableViewCell class] heightOfLabel:self.strI andTextFont:[UIFont systemFontOfSize:15 *kWidth] andTextWidth:self.frame.size.width - 20 *kWidth]);
    self.btnName.frame = CGRectMake(10 * kWidth, self.labelDes.frame.origin.y + self.labelDes.frame.size.height + 5 *kWidth, 150 *kWidth, 20 *kHeight);

}

-(void)setModel:(TravelTopicDetailsModel *)model
{
    if (_model != model) {
        [_model release];
        _model  = [model retain];
    }
    
    self.wd =  [model.image_width floatValue];
    self.kh = [model.image_height floatValue];

    self.strI = model.DES;
    
    for (NSString *title in [model.description_user_ids allKeys]) {
        if ([self.strI containsString:title]) {
            
            self.strI = [self.strI stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@ ",title] withString:[NSString stringWithFormat:@"%@ ",[model.description_user_ids objectForKey:title]]];
        }
        
    }

    if (![self.strI isKindOfClass:[NSNull class]] && [self.strI containsString:@"#"])
    {
        NSArray *arr = [self.strI componentsSeparatedByString:@"#"];
        
        NSString *str = [NSString stringWithFormat:@"#%@#",arr[1]];
        
        NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]
                                                initWithString:self.strI];
        [attString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0, str.length)];
        
        [self.labelDes setAttributedText:attString];
    }else{
        self.labelDes.text =self.strI;
    }

    [self.btnPic sd_setBackgroundImageWithURL:[NSURL URLWithString:model.image_url] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"Travel_place"]];
    

        [self.btnName setTitle:[model.attraction objectForKey:@"name"]forState:UIControlStateNormal];
    
                self.btnName.tag = 10000 + [[model.attraction objectForKey:@"id"] integerValue];
        
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
